﻿using System.Windows;
using System.Windows.Input;
using test.ViewModels;
using test.Views;
using System.Drawing; // 图标
using System.Windows.Forms; // 引入托盘控件

namespace test
{
    public partial class MainWindow : Window
    {
        private MainViewModel _viewModel;
        private NotifyIcon notifyIcon = null;
        private string win_close;

        public MainWindow()
        {
            InitializeComponent();
            _viewModel = App.MainViewModel;
            this.DataContext = _viewModel;
            initialTray();
            Dispatcher.BeginInvoke(new Action(() =>
            {
                _viewModel.initialclose();   // 子窗口的初始化代码
            }));

        }





        //-------------------------------------------------------------------------------------------------------------------
        public void initialTray()
        {
            
            notifyIcon = new NotifyIcon();
            notifyIcon.BalloonTipText = "东东电竞运行中...";//托盘气泡显示内容
            notifyIcon.Text = "dongdong";
            notifyIcon.Visible = true;//托盘按钮是否可见
            string iconPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "youricon.ico");
            //重要提示：此处的图标图片在resouces文件夹。可是打包后安装发现无法获取路径，导致程序死机。建议复制一份resouces文件到UI层的bin目录下，确保万无一失。
            notifyIcon.Icon = new System.Drawing.Icon(iconPath);//托盘中显示的图标
            notifyIcon.ShowBalloonTip(500);//托盘气泡显示时间
            //双击事件
            //_notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
            //鼠标点击事件
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
            //右键菜单--退出菜单项
            System.Windows.Forms.ToolStripMenuItem exit = new System.Windows.Forms.ToolStripMenuItem("关闭");
            exit.Click += new EventHandler(exit_Click);

            // 创建上下文菜单并将退出菜单项添加到其中
            System.Windows.Forms.ContextMenuStrip contextMenu = new System.Windows.Forms.ContextMenuStrip();
            contextMenu.Items.Add(exit);

            // 关联托盘控件
            notifyIcon.ContextMenuStrip = contextMenu;
            //窗体状态改变时触发
            this.StateChanged += MainWindow_StateChanged;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!这句话有BUG
        }


        // 托盘图标鼠标单击事件
        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //鼠标左键，实现窗体最小化隐藏或显示窗体
            if (e.Button == MouseButtons.Left)
            {
                if (this.Visibility == Visibility.Visible)
                {
                    this.Visibility = Visibility.Hidden;
                    //解决最小化到任务栏可以强行关闭程序的问题。
                    this.ShowInTaskbar = false;//使Form不在任务栏上显示
                }
                else
                {
                    this.Visibility = Visibility.Visible;
                    //解决最小化到任务栏可以强行关闭程序的问题。
                    this.ShowInTaskbar = true;//使Form不在任务栏上显示
                    this.Activate();
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                //object sender = new object();
                // EventArgs e = new EventArgs();
                exit_Click(sender, e);//触发单击退出事件
            }
        }

        // 窗体状态改变时候触发
        private void SysTray_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }

        // 退出选项
        private void exit_Click(object sender, EventArgs e)
        {
            if (System.Windows.MessageBox.Show("确定退出吗?",
                                               "dongdong",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.Yes) == MessageBoxResult.Yes)
            {
                //System.Windows.Application.Current.Shutdown();
                System.Environment.Exit(0);
            }
        }


        // 窗口状态改变，最小化托盘
        private void MainWindow_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }//-----------------_________________————————————————————————————————————————————————————————————————

       
    }
}
